package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.programme.ParameterInfoRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created By Dai On 2018/4/28
 * Remark:
 */
@Service
public class ParameterInfoService {
    @Autowired
    private ParameterInfoRepository parameterInfoRepository;

    /**
     * 工程配置 -> 对象管理 -> 模组温度配置 -> 配置
     * 找到所有的温度列表
     */
    public List<ParameterInfo> findParameterInfoJustTemperature() {
        return parameterInfoRepository.findParameterInfoJustTemperature();
    }

    /**
     * 参数测点
     *
     * @param pageNum
     * @return
     */
    @Cacheable(value = "projectPage")
    public Page<ParameterInfo> findAllParameterInfos(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ParameterInfo> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<ParameterInfo> page = parameterInfoRepository.findAll(mySpecifications, pageable);
        return page;
    }

    @Cacheable(value = "projectPage")
    public Page<ParameterInfo> findAllParameterInfos(int pageNum, HttpServletRequest request, int pageSize) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ParameterInfo> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, pageSize, sort);
        Page<ParameterInfo> page = parameterInfoRepository.findAll(mySpecifications, pageable);
        return page;
    }

    // 保存
    @CacheEvict(value = "projectPage", allEntries = true)
    public void saveParameterInfo(String id, String code, String name, Integer objName, Integer objType, Integer propertyType, String unit,
                                  Integer source, Integer isShow, String esName, Long esAggsMethod) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        ParameterInfo parameterInfo = new ParameterInfo();
        String uuid = "";
        if (id != null) {
            parameterInfo = findById(id);
            parameterInfo.setCreateUser(new User(shiroUser.getId()));
            parameterInfo.setUpdateTime(new Date());
        } else {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            parameterInfo.setId(uuid);
            parameterInfo.setCreateTime(date);
            parameterInfo.setCreateUser(new User(shiroUser.getId()));
            parameterInfo.setIsValid(1);
        }

        parameterInfo.setCode(code);
        parameterInfo.setName(name);
        parameterInfo.setObjName(objName);
        parameterInfo.setObjType(objType);

        parameterInfo.setPropertyType(propertyType);
        parameterInfo.setUnit(unit);
        parameterInfo.setSource(source);
        if (isShow == null) {
            isShow = 0;
        }
        parameterInfo.setIsShow(isShow);
        parameterInfo.setEsName(esName);
        parameterInfo.setEsAggsMethod(esAggsMethod);

        parameterInfoRepository.save(parameterInfo);
    }

    //参数测点删除
    public void delParameterInfo(String[] ids) {
        List<ParameterInfo> parameterInfos = parameterInfoRepository.findAll(Arrays.asList(ids));
        for (ParameterInfo parameterInfo : parameterInfos) {
            parameterInfo.setIsValid(0);
        }
        parameterInfoRepository.save(parameterInfos);
    }

    //参数测点编辑框回显数据
    public ParameterInfo findById(String id) {
        ParameterInfo code = parameterInfoRepository.findOne(id);
        return code;
    }

    //文件数据导出
    public List<ParameterInfo> findAllParameterInfo() {
        return parameterInfoRepository.findByIsValid();
    }

    //数据导入
    public String saveImport(List<String[]> list,String flag) {
        List<ParameterInfo> allCode = parameterInfoRepository.findAllDataByCodeAndisValid();
        flag = "0";
        //遍历Excel表中的数据
        for (String[] info : list) {
            if(allCode.contains(info[0])){
                flag = "1";
                return flag;
            }
            ParameterInfo pinfo = new ParameterInfo();
            pinfo.setCode(info[0]);
            pinfo.setName(info[1]);
            pinfo.setObjName(Integer.valueOf(info[2]));
            pinfo.setObjType(Integer.valueOf(info[3]));
            pinfo.setPropertyType(Integer.valueOf(info[4]));
            pinfo.setUnit(info[5]);
            pinfo.setSource(Integer.valueOf(info[6]));
            pinfo.setIsShow(Integer.valueOf(info[7]));
            pinfo.setEsName(info[8]);
            pinfo.setEsAggsMethod(Long.valueOf(info[9]));


            pinfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            pinfo.setCreateTime(new Date());
            MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();

            pinfo.setCreateUser(new User(shiroUser.getId()));
            pinfo.setIsValid(1);

            parameterInfoRepository.save(pinfo);
        }
        return flag;
    }

    //查询code
    public List<ParameterInfo> findCode() {
        return parameterInfoRepository.findCode();
//        List<ParameterInfo> pinfo =  parameterInfoRepository.findCode();
//        return pinfo;
    }
}
